<!-- 员工资料编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改员工资料':'添加员工资料'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">
        <el-form-item label="流水号:" prop="liusNo">
          <el-input-number
            :min="0"
            v-model="form.liusNo"
            placeholder="请输入流水号"
            controls-position="right"
            class="ele-fluid ele-text-left"/>
        </el-form-item>
        <el-form-item
          label="员工编号:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.zhiyNo"
            placeholder="请输入员工编号"
            clearable/>
        </el-form-item>
        <el-form-item
          label="员工姓名:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.zhiyName"
            placeholder="请输入员工姓名"
            clearable/>
        </el-form-item>
        <el-form-item
          label="是否活动:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.beactive"
            placeholder="请输入是否活动"
            clearable/>
        </el-form-item>
        <el-form-item
          label="更新时间:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.gengxTime"
            placeholder="请输入更新时间"
            clearable/>
        </el-form-item>
        <el-form-item
          label="yez_id:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.yezId"
            placeholder="请输入yez_id"
            clearable/>
        </el-form-item>
        <el-form-item
          label="状态:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.zt"
            placeholder="请输入状态"
            clearable/>
        </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'C37InfJcZhiydocEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        liusNo: [
          {required: true, message: '请输入流水号', trigger: 'blur'}
        ],
        zhiyNo: [
          {required: true, message: '请输入员工编号', trigger: 'blur'}
        ],
        zhiyName: [
          {required: true, message: '请输入员工姓名', trigger: 'blur'}
        ],
        beactive: [
          {required: true, message: '请输入是否活动', trigger: 'blur'}
        ],
        gengxTime: [
          {required: true, message: '请输入更新时间', trigger: 'blur'}
        ],
        yezId: [
          {required: true, message: '请输入yez_id', trigger: 'blur'}
        ],
        zt: [
          {required: true, message: '请输入状态', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/c37infjczhiydoc/edit' : '/c37infjczhiydoc/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
